﻿// 菜单权限维护ExtJs
function Infra_Menu() {
	var ActionStore = new Ext.data.JsonStore({
		url: '/InfraMenu/GetActionQueryList',
		root: 'data',
		totalProperty: 'total',
		id: 'Id',
		fields: [{
			name: 'ActionName',
			type: 'string'
		}, {
			name: 'ControllerName',
			type: 'string'
		}, {
			name: 'Description',
			type: 'string'
		}]
	});
	// 定义表单用Reader
	var FormReader = new Ext.data.JsonReader({
		root: 'data'
	}, [{
		name: 'Name', // 名称
		type: 'string'
	}, {
		name: 'TreeCode', // 代码
		type: 'string'
	}, {
		name: 'ActionName',
		type: 'string'
	}, {
		name: 'ControllerName',
		type: 'string'
	}, {
		name: 'Index',
		type: 'int'
	}, {
		name: 'Description',
		type: 'string'
	}, {
		name: 'URL',
		type: 'string'
	}, {
		name: 'BigIcon',
		type: 'string'
	}, {
		name: 'SmallIcon',
		type: 'string'
	}, {
		name: 'Leaf',
		type: 'string'
	}, {
		name: 'IsFunction',
		type: 'string'
	}, {
		name: 'IsExtPage',
		type: 'string'
	}]);

	var record = Ext.data.Record.create([{
		name: 'Name'
	}, {
		name: 'TreeCode'
	}, {
		name: 'Id',
		type: 'string'
	}, {
		name: 'ParentId',
		type: 'auto'
	}, {
		name: 'Leaf',
		type: 'bool'
	}, {
		name: 'Level',
		type: 'int'
	}, {
		name: 'ActionName',
		type: 'string'
	}, {
		name: 'ControllerName',
		type: 'string'
	}, {
		name: 'Index',
		type: 'int'
	}, {
		name: 'Description',
		type: 'string'
	}, {
		name: 'URL',
		type: 'string'
	}, {
		name: 'BigIcon',
		type: 'string'
	}, {
		name: 'SmallIcon',
		type: 'string'
	}, {
		name: 'IsFunction',
		type: 'bool'
	}, {
		name: 'IsExtPage',
		type: 'bool'
	}, {
		type: 'date',
		name: 'CreateTime'
	}]);

	var store = new Ext.ux.maximgb.tg.AdjacencyListStore({
		reader: new Ext.data.JsonReader({
			id: 'Id',
			root: 'data'
		}, record),
		url: '/InfraMenu/GetMenuGrid'
	});
	store.setDefaultSort('Index', 'ASC');

	var rootReocdData = {
		"Name": "所有菜单",
		"Code": "",
		"Id": "0",
		"ParentId": null,
		"Leaf": null,
		"Level": 0,
		"IsExtPage": null,
		"IsFunction": null
	};

	store.isLoadedNode = function (rc) {
		return true;
	}
	var rootReocd = new store.recordType(rootReocdData, 0);
	store.insert(0, rootReocd);

	var showEditFormWinFn = function (formReader, title, id, level) {
		// 定义变量 是否更新操作标记 更新：true 添加：false
		var updateEdit = title == "修改菜单权限";
		var leafflag = false; // 定义是否叶子菜单复选框disabled属性值
		if (updateEdit) {
			var r = store.getById(id);
			var leaf = r.get("Leaf");
			if (!leaf) {
				if (store.hasChildNodes(r)) {
					leafflag = true;
				}
			}
		}
		// 定义表单验证成功后的提交参数及返回处理
		var formSubmitArgs = {
			url: '/InfraMenu/SaveOrUpdate',
			params: {
				'Id': (updateEdit) ? id : "",
				'ParentId': (updateEdit) ? "" : id,
				'Level': level
			},
			success: function (form, action) {
				win.destroy(); // 关闭窗口
				store.reload({
					callback: function () {
						var rc = this.getById(id);
						grid.addBtn.setDisabled(rc.get("Leaf") == true || rc.get("Level") >= 2);
						grid.delBtn.setDisabled(rc.get("Level") == 0 || this.hasChildNodes(store.getById(id)));
					}
				});
			},
			failure: function (form, action) {
				JsHelper.ShowError(action.result.msg);
			}
		};
		// 定义获得表单初始加载数据的请求参数
		var formLoadArgs = (updateEdit) ? {
			url: '/InfraMenu/GetMenuById',
			params: {
				'Id': id
			},
			waitMsg: '数据加载中...'
		} : null;
		// 定义表单第一列的组件对象集合 默认xtype为textfield
		var firstColumn = [{
			fieldLabel: '菜单名<span style="color: red;" >*</span>',
			allowBlank: false,
			name: 'Name',
			disabled: updateEdit,
			rvOptions: {
				url: '/InfraMenu/IsFieldExits',
				params: {
					'Id': "-1",
					'FieldName': 'Name'
				}
			},
			plugins: [Ext.ux.plugins.RemoteValidator]
		}, {
			fieldLabel: 'Action名称',
			disabled: updateEdit,
			hiddenName: 'ActionName',
			xtype: 'sicombo',
			store: ActionStore,
			mode: 'remote',
			valueField: 'ActionName',
			displayField: 'ActionName',
			listWidth: 650,
			tpl: new Ext.XTemplate(
							'<div class="x-grid3"><div><table>',
							'<thead class="x-grid3-header"><tr class="x-grid3-hd-row">',
							'<td class="x-grid3-hd x-grid3-cell x-grid3-hd-inner" style="width: 150px;color: blue;">Action名称</td>',
							'<td class="x-grid3-hd x-grid3-cell" style="width: 200px;color: blue;">Controller名称</td>',
							'<td class="x-grid3-hd x-grid3-cell" style="width: 300px;color: blue;">描述</td>',
							'</tr></thead><tbody>',
							'<tpl for=".">',
							'<tr class="x-combo-list-item">',
							'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{ActionName}</div></td>',
							'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{ControllerName}</div></td>',
							'<td class="x-grid3-col x-grid3-cell"><div class="x-grid3-cell-inner"  style="font-size:12px;">{Description}</div></td>',
							'</tr>', '</tpl>', '</tbody></table></div></div>'),
			listeners: {
				'beforesetvalue': function (r) {
					Ext.getCmp("Infra_Menu_EditFormWin_ControllerName")
									.setValue(r.get('ControllerName'));
					Ext.getCmp("Infra_Menu_EditFormWin_Description")
									.setValue(r.get('Description'));
					Ext.Ajax.request({
						url: '/InfraMenu/GetUrl',
						params: {
							controllerName: r.get('ControllerName'),
							actionName: r.get('ActionName')
						},
						success: function (response, opts) {
							var result = Ext.util.JSON.decode(response.responseText);
							Ext.getCmp("Infra_Menu_EditFormWin_URL").setValue(result);

						}
					});
				},
				'afterclearValue': function () {
					Ext.getCmp("Infra_Menu_EditFormWin_ControllerName").setValue("");
					Ext.getCmp("Infra_Menu_EditFormWin_Description").setValue("");
					Ext.getCmp("Infra_Menu_EditFormWin_URL").setValue("");
				}
			}
		}, {
			fieldLabel: 'Controller名称',
			id: 'Infra_Menu_EditFormWin_ControllerName',
			readOnly: true,
			name: 'ControllerName'
		}, {
			fieldLabel: '排序',
			id: 'Infra_Menu_EditFormWin_Index',
			name: 'Index',
			xtype: 'numberfield',
			minValue: 0,
			maxValue: 1000,
			allowDecimals: false
		}, {
			fieldLabel: '描述',
			id: 'Infra_Menu_EditFormWin_Description',
			readOnly: true,
			name: 'Description'
		}, {
			fieldLabel: 'URL',
			id: 'Infra_Menu_EditFormWin_URL',
			readOnly: true,
			name: 'URL'
		}];
		// 定义表单第二列的组件对象集合
		var secondColumn = [{
			fieldLabel: '代码<span style="color: red;" >*</span>',
			allowBlank: false,
			name: 'TreeCode',
			disabled: updateEdit,
			rvOptions: {
				url: '/InfraMenu/IsFieldExits',
				params: {
					'Id': "-1",
					'FieldName': 'TreeCode'
				}
			},
			plugins: [Ext.ux.plugins.RemoteValidator]
		}, {
			fieldLabel: '大图标',
			name: 'BigIcon'
		}, {
			fieldLabel: '小图标',
			name: 'SmallIcon'
		}, {
			xtype: 'checkbox',
			fieldLabel: '是否叶子菜单',
			disabled: leafflag,
			inputValue: true,
			name: 'Leaf'
		}, {
			xtype: 'checkbox',
			fieldLabel: '是否EXT页面',
			inputValue: true,
			name: 'IsExtPage'
		}, {
			xtype: 'checkbox',
			fieldLabel: '是否界面菜单',
			inputValue: true,
			name: 'IsFunction'
		}];

		// 定义表单
		var form = new Ext.FormPanel({
			border: false,
			waitMsgTarget: true,
			labelWidth: 90,
			frame: true,
			bodyStyle: 'padding:5px 5px 0',
			reader: formReader,
			labelAlign: 'right',
			items: [{
				layout: 'column',
				items: [{
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 250
					},
					items: firstColumn
				}, {
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 250
					},
					items: secondColumn
				}]
			}],
			buttonAlign: 'center',
			buttons: [new Ext.ux.SubmitBtn({
				handler: function () {
					if (form.getForm().isValid()) {
						form.getForm().submit(formSubmitArgs); // formSubmitArgs
						// 引用之前定义的请求参数变量
					}
				}
			}), new Ext.ux.CancelBtn({
				handler: function () {
					win.destroy();
				}
			})]
		});

		// 定义显示窗口
		var win = new Ext.Window({
			title: title,
			iconCls: 'application_form',
			width: 800,
			resizable: false,
			constrain: true,
			autoHeight: true,
			modal: true,
			closeAction: 'close',
			plain: true,
			items: [form]
		});
		win.show();
		// 判断是否为表单加载初始数据
		if (formLoadArgs) {
			form.load(formLoadArgs); // formLoadArgs 引用之前定义表单数据加载的请求参数变量
		}
	}
	// 工具栏
	var tbar = [{
		text: '添加项',
		cls: "x-btn-text-icon",
		ref: '../addBtn',
		disabled: true,
		icon: "/Content/icons/add.png",
		handler: function () {
			var r = grid.getSelectionModel().getSelected();
			var id = r.get("Id");
			var level = r.get("Level");
			showEditFormWinFn(FormReader, "添加菜单权限", id, level);
		}
	}, '-', {
		text: '删除项',
		ref: '../delBtn',
		disabled: true,
		cls: "x-btn-text-icon",
		icon: "/Content/icons/delete.png",
		handler: function () {
			JsHelper.DelConfirm(function (btn) {
				if (btn == 'yes') {
					var r = grid.getSelectionModel().getSelected();
					var id = r.get("Id");
					Ext.Ajax.request({
						url: '/InfraMenu/DeleteDicCategory',
						params: {
							'Id': id
						},
						success: function (response, opts) {
							var result = Ext.util.JSON.decode(response.responseText);
							if (result.success == false) {
								JsHelper.ShowError(result.msg);
							} else {
								store.removeAll();
								store.insert(0, rootReocd);
								store.reload({
									callback: function () {
										grid.getSelectionModel().selectFirstRow();
										grid.addBtn.setDisabled(false);
										grid.delBtn.setDisabled(true);
									}
								});
							}
						}
					});
				}
			});
		}
	}, '-', {
		xtype: "tbbutton",
		minWidth: 80,
		text: "双击修改项",
		cls: "x-btn-text-icon",
		icon: "/Content/icons/pencil.png",
		disabled: true
	}];

	var grid = new Ext.ux.maximgb.tg.GridPanel({
		region: "center",
		enableColumnMove: false,
		frame: true,
		border: true,
		layout: "fit",
		loadMask: true,
		tbar: tbar,
		store: store,
		master_column_id: 'Name',
		columns: [{
			id: 'Name',
			header: "菜单名",
			width: 160,
			dataIndex: 'Name'
		}, {
			header: "代码",
			width: 70,
			dataIndex: 'TreeCode'
		}, {
			header: "Controller名称",
			dataIndex: 'ControllerName'
		}, {
			header: "Action名称",
			dataIndex: 'ActionName'
		}, {
			header: "排序",
			dataIndex: 'Index',
			width: 60,
			sortable: true
		}, {
			header: "描述",
			dataIndex: 'Description'
		}, {
			header: "大图标",
			dataIndex: 'BigIcon'
		}, {
			header: "小图标",
			dataIndex: 'SmallIcon'
		}, {
			header: "URL",
			dataIndex: 'URL'
		}, {
			header: "是否叶子菜单",
			dataIndex: 'Leaf',
			renderer: function (v) {
				if (v == null) {
					return "";
				}
				if (v) {
					return "是";
				} else {
					return "否";
				}
			}
		}, {
			header: "是否EXT界面",
			dataIndex: 'IsExtPage',
			renderer: function (v) {
				if (v == null) {
					return "";
				}
				if (v) {
					return "是";
				} else {
					return "否";
				}
			}
		}, {
			header: "是否界面菜单",
			dataIndex: 'IsFunction',
			renderer: function (v) {
				if (v == null) {
					return "";
				}
				if (v) {
					return "是";
				} else {
					return "否";
				}
			}
		}, {
			xtype: 'datecolumn',
			format: 'Y年m月d日 H:i:s',
			header: "创建时间",
			width: 150,
			dataIndex: 'CreateTime'
		}],
		stripeRows: true,
		listeners: {
			"rowdblclick": function (g, rowIndex, e) {
				var store = g.getStore()
				var id = store.getAt(rowIndex).get("Id");
				var level = store.getAt(rowIndex).get("Level");
				if (level != 0) {
					showEditFormWinFn(FormReader, "修改菜单权限", id, level);
				}
			},
			"rowclick": function (g, rowIndex, e) {
				var store = g.getStore()
				var level = store.getAt(rowIndex).get("Level");
				var id = store.getAt(rowIndex).get("Id");
				var leaf = store.getAt(rowIndex).get("Leaf");
				var IsFunction = store.getAt(rowIndex).get("IsFunction");
				//grid.addBtn.setDisabled(level >= 2 || leaf == true);
				grid.addBtn.setDisabled(IsFunction == true);
				grid.delBtn.setDisabled(level == 0 || store.hasChildNodes(store.getAt(rowIndex)));
			}
		}
	});
	// 加载
	JsHelper.ExtTabDoLayout(grid);
	grid.store.load({
		add: true,
		callback: function () {
			this.expandNode(rootReocd);
		}
	});
}
Infra_Menu();